﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SEO.Business;
using SEO.Entity;

/// <summary>
/// 文章提成统计
/// </summary>
public partial class ArticleReport : AdminPage
{
    protected int nBeginYear = 2013;

    /// <summary>
    /// 开始时间
    /// </summary>
    protected string BeginDateTime
    {
        get { return ViewState["BeginDateTime"] == null ? "" : ViewState["BeginDateTime"].ToString(); }
        set { ViewState["BeginDateTime"] = value; }
    }

    /// <summary>
    /// 结束时间
    /// </summary>
    protected string EndDateTime
    {
        get { return ViewState["EndDateTime"] == null ? "" : ViewState["EndDateTime"].ToString(); }
        set { ViewState["EndDateTime"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitConfig();
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.ArticleReport;
    }

    /// <summary>
    /// 加截配置
    /// </summary>
    protected void InitConfig()
    {
        BeginDateTime = DateTime.Now.ToString("yyyy-MM-01");
        EndDateTime = Convert.ToDateTime(BeginDateTime).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
        txtBeginDateTime.Text = BeginDateTime;
        txtEndDateTime.Text = EndDateTime;

        //int nNowYear = DateTime.Now.Year;
        //int nTemYear = nBeginYear;
        //for (int i = 0; i <= (nNowYear - nBeginYear); i++)
        //{
        //    nTemYear = nBeginYear + i;
        //    ddlBeginYear.Items.Add(new ListItem(nTemYear.ToString(), nTemYear.ToString()));
        //    ddlEndYear.Items.Add(new ListItem(nTemYear.ToString(), nTemYear.ToString()));
        //}
        //SysConfig sysConfig = Common.GetSysConfig();
        //if (sysConfig != null)
        //{
        //    ltlBeginDay.Text = sysConfig.AccountDay.Value.ToString("00");
        //    ltlEndDay.Text = sysConfig.AccountDay.Value.ToString("00");
        //    DateTime dAccount = Common.GetAccountDay(sysConfig);
        //    ddlBeginYear.SelectedValue = dAccount.ToString("yyyy");
        //    ddlBeginMonth.SelectedValue = dAccount.ToString("MM");
        //    dAccount = dAccount.AddMonths(1);
        //    ddlEndYear.SelectedValue = dAccount.ToString("yyyy");
        //    ddlEndMonth.SelectedValue = dAccount.ToString("MM");
        //}
    }

    protected void InitData()
    {
        //信息列表绑定
        DataTable dt = new BisAdminInfo().GetAllUserByLock();
        dt.Columns.Add("NewsMomey", typeof(string));
        dt.Columns.Add("LinkMomey", typeof(string));
        dt.Columns.Add("OurWebMomey", typeof(string));
        dt.Columns.Add("AllMomey", typeof(string));
        if (dt != null && dt.Rows.Count > 0)
        {
            noData.Style["display"] = "none";
            DateTime dBeginTime = Convert.ToDateTime(BeginDateTime);
            DateTime dEndTime = Convert.ToDateTime(EndDateTime).AddDays(1).AddSeconds(-1);
            DataTable dtReport = new BisArticleSEO().CountByMonth(dBeginTime, dEndTime);
            foreach (DataRow dr in dt.Rows)
            {
                dr["NewsMomey"] = 0;
                dr["LinkMomey"] = 0;
                dr["OurWebMomey"] = 0;
                dr["AllMomey"] = 0;
                DataRow[] drReportList = dtReport.Select("AdminID="+dr["ID"].ToString());
                if (drReportList.Length > 0)
                {
                    double dAllMomey = 0;
                    foreach (DataRow drReport in drReportList)
                    {
                        dAllMomey += Convert.ToDouble(drReport["AllMomey"]);
                        switch (Convert.ToInt32(drReport["UrlType"]))
                        {
                            case 1:
                                dr["NewsMomey"] = drReport["AllMomey"];
                                break;
                            case 2:
                                dr["LinkMomey"] = drReport["AllMomey"];
                                break;
                            case 3:
                                dr["OurWebMomey"] = drReport["AllMomey"];
                                break;
                        }
                        dr["AllMomey"] = dAllMomey;
                    }
                }
                
            }

        }
        else
        {
            noData.Style["display"] = "block";
        }
        rptUser.DataSource = dt;
        rptUser.DataBind();
        dt.Dispose();
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        BeginDateTime = txtBeginDateTime.Text.Trim();
        EndDateTime = txtEndDateTime.Text.Trim();
        InitData();
    }
}